﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using Resources;
using System.Data;
using System.DirectoryServices.Protocols;
using System.Security.Cryptography.X509Certificates;
using System.DirectoryServices;
using System.Net;

public partial class login : System.Web.UI.Page
{
    class_user _user;
   
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            setLang();
            if (Session["usename"] != null)
            {
                Response.Redirect("Default.aspx");
            }
        }
    }

    private void setLang()
    {
        btn_login.Text = "Đăng nhập";
    }

    protected void btn_login_click(object sender, EventArgs e)
    {
        _user = new class_user();
     //   string ip = _user.GetIpUser();

        string str_user = class_string.clear_inject(class_string.clear_inject(txt_user.Text.Trim()));
        string str_pass = class_string.clear_inject(class_string.clear_inject(txt_pass.Text.Trim()));
        string error = "";

        if (str_user.IndexOf("@bkav.com") ==-1)
        {
            str_user += "@bkav.com";        
        }

        if (str_user == "")
        {
            error = "Bạn chưa nhập tên truy cập" + "<br/>";
        }else if(str_pass =="")
        {
            error = "Bạn chưa nhập mật khẩu" + "<br/>";
        }else
        {
           if (CheckLoginAD(str_user, str_pass))
            //if(1==1)
            {
                if (!_user.LoginUser(str_user))
                {
                    error = "Tài khoản chưa được phân quyền" + "<br/>";
                    lblError.Attributes.Add("style", "color:red");
                    lblError.Text = "<b>" + "Lỗi" + ":</b><br />" + error;
                }
            }
            else
            {
                error = "Tài khoản không đúng" + "<br/>";            
            }
        }

        if (error == "")
        {           
            Response.Redirect("Default.aspx");
        }
        else
        {
            lblError.Attributes.Add("style", "color:red");
            lblError.Text = "<b>" + "Lỗi" + ":</b><br/>" + error;
        }
    }

    public bool CheckLoginAD(string username, string password)
    {


        //try
        //{
        //    //LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier("10.3.10.151", 636));
        //    LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier("ldaps.bkav.com", 636));

        //    connection.SessionOptions.ProtocolVersion = 3;

        //    connection.AuthType = AuthType.Basic;

        //    connection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback((con, cer) => true);

        //    connection.Credential = new NetworkCredential(username, password);

        //    connection.SessionOptions.SecureSocketLayer = true;

        //    connection.Bind();

        //}
        //catch (System.Exception ex)
        //{
        //    string sErr = ex.Message;
        //    return false;
        //}

        return true;
    }
    public bool ServerCallBack(LdapConnection connection, X509Certificate certificate)
    {
        return true;
    }

}